package com.zygh.lz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zygh.lz.dao.app.AppTrailerAbnormalCauseMapper;
import com.zygh.lz.entity.app.AppTrailerAbnormalCause;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ljhao
 * @CreateTime: 2023-03-25  09:19
 * @Description: 拖车记录异常表
 * @Version: 1.0
 **/
@Slf4j
@Service
@Transactional
public class AppTrailerAbnormalCauseService extends ServiceImpl<AppTrailerAbnormalCauseMapper, AppTrailerAbnormalCause> {

    @Resource
    AppTrailerAbnormalCauseMapper appTrailerAbnormalCauseMapper;



    /**
     * 获取拖车记录异常列表
     *
     * @param deps      部门列表
     * @param startTime 开始时间
     * @param endTime   接受时间
     * @return Map<String, Object>
     * {
     * "twoSubmissionsHaveBeenMadeOnThisDate": 2,
     * "duplicateSubmissions": 2,
     * "thePhotoSimilarityIsTooHigh": 2,
     * "fiveSubmissionsHaveBeenMadeThisMonth": 1,
     * "theTwoPhotosAreInconsistent": 2
     * },
     */
    public Map<String, Object> getAbnormalCount(List<Integer> deps, String startTime, String endTime) {
        // 根据部门列表查询异常统计数据
        List<Map<String, Object>> abnormalCount = appTrailerAbnormalCauseMapper.getAbnormalCount(deps, startTime, endTime);
        // 将mapList转化成 键值对
        HashMap<String, Object> hashMap = new HashMap<>();
        abnormalCount.forEach(map -> {
            hashMap.put(map.get("type").toString(), map.get("count"));
        });
        // 整理结果 将没有结果的数据填充为0
        HashMap<String, Object> map = new HashMap<>();
        for (int i = 1; i < 6; i++) {
            Object s = hashMap.get(String.valueOf(i));
            if (s == null) {
                s = 0;
            }
            map.put(parseAbnormalType(i), s);
        }
        return map;
    }

    public String parseAbnormalType(int type) {
        //1：两次照片不一致，2：重复提交，3：本日已提交2次，4：本月已提交5次，5：照片相似度过高
        switch (type) {
            case 1:
                return "theTwoPhotosAreInconsistent";
            case 2:
                return "duplicateSubmissions";
            case 3:
                return "twoSubmissionsHaveBeenMadeOnThisDate";
            case 4:
                return "fiveSubmissionsHaveBeenMadeThisMonth";
            case 5:
                return "thePhotoSimilarityIsTooHigh";
        }
        return "";
    }



}
